package com.xqls.server.dto;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.OperationRecordBean;
import com.xqls.server.netty.DecoderUtil;
import io.netty.buffer.ByteBuf;
import org.apache.log4j.Logger;

import java.io.Serializable;


public class SoketCommonDto implements Serializable {
    private static Logger logger = Logger.getLogger(SoketCommonDto.class);
    private static final String HEAD_STR = "aa";//针头

    private static final String END_STR = "a5";//针尾

    private  String deviceName;
    /**
     * 帧头
     */
    private String head= "aa";
    /**
     * 帧长度
     */
    private Integer length;
    /**
     * 指令标识码
     */
    private String code;

    /**
     * 设备地址
     */
    private String adress;

    /**
     * 帧尾
     */
    private String end="a5";

    private OperationRecordBean activeOperationRecordBean;

    public OperationRecordBean getActiveOperationRecordBean() {
        return activeOperationRecordBean;
    }

    public void setActiveOperationRecordBean(OperationRecordBean activeOperationRecordBean) {
        this.activeOperationRecordBean = activeOperationRecordBean;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }


    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }


    public static SoketCommonDto getDecode(ByteBuf in) {
        if (in.readableBytes() <= 0)
            return null;

        String head = DecoderUtil.toHex(in.readByte());
        if (!HEAD_STR.equals(head)) {
            return null;
        }
        int length = DecoderUtil.byteToInt(in.readByte());

        String code = DecoderUtil.toHex(in.readByte());
        DtoEnum dtoEnum = DtoEnum.find(code);
        if (dtoEnum == null) return null;
        try {
            String adress = DecoderUtil.threeToHex(in);
            SoketCommonDto dto = (SoketCommonDto) dtoEnum.getClss().newInstance();
            dto.setHead(head);
            dto.setCode(code);
            dto.setLength(length);
            dto.setAdress(adress);
            dto.decode(in, dto);
            return dto;
        } catch (Exception e) {
            logger.error("解码客户端信息异常", e);
        }
        return null;
    }


    protected void decode(ByteBuf in, SoketCommonDto soketCommonDto) {
        String end = DecoderUtil.toHex(in.readByte());
        if (!END_STR.equals(end)) {
            throw new RuntimeException("结束的字节不正确");
        }
        soketCommonDto.setEnd(end);
    }

    public   void getEncode(ByteBuf in) {
        try {
            in.writeBytes(DecoderUtil.hexStringToBytes(this.getHead()));
            in.writeByte(DecoderUtil.intToByte(this.getLength()));
            in.writeBytes(DecoderUtil.hexStringToBytes(this.getCode()));
            in.writeBytes(DecoderUtil.hexStringToBytes(this.getAdress()));
            this.encode(in);
            logger.info("发送指令"+this.toString());
        } catch (Exception e) {
            logger.error("编码客户端信息异常", e);
        }
    }

    public void encode(ByteBuf byteBuf) {
        byteBuf.writeBytes(DecoderUtil.hexStringToBytes(end));
    }

    public void operate() {


    }


    public String  toString (){
        return JSONObject.toJSONString(this);
    }
}
